import md5 from "js-md5"
import {SignSecretKey} from './config'
import {getUrlKey,getLocVal} from './utils'
function uuid() {
    function S4() {
       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    }
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}


export function encrypt(model={}) {
    var data = {
        timestamp: new Date().valueOf(),
        nonce:uuid(),
        clientSource:'h5',
        token:model.token||getUrlKey('token')||getLocVal('token'),
        sign: '',
    };
    model = objMerger(model, data);
    var noSet = [];
    for (var key in model) {
        if (key != "sign" && model[key]!=undefined && model[key]!='') {
            noSet.push(key);
        }
    }
    noSet.sort();
    var sign = '';
    for (var i = 0; i < noSet.length; i++) {
        sign += noSet[i] + "=" + model[noSet[i]] + "&"
    }
    // console.log(sign)
    sign = sign.substring(0, sign.length - 1);
    return md5(sign+SignSecretKey).toLowerCase()
    
}
function objMerger(data1, data2) {
    for (var r in data2) {
        data1[r] = data2[r];
    }
    return data1;
}

